﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TestSystem.BLL;
using TestSystem.BLL.TestSystem;

public partial class Admin_ManageAnswers : System.Web.UI.Page
{
    private int quizID = 0;
    public int QuizID
    {
        get
        {
            if (quizID == 0 && Request.QueryString["quizID"] != null)
                quizID = Convert.ToInt32(Request.QueryString["quizID"]);
            return quizID;
        }
    }
    private int testID = 0;
    public int TestID
    {
        get
        {
            if (testID == 0 && Request.QueryString["testID"] != null)
                testID = Convert.ToInt32(Request.QueryString["testID"]);
            return testID;
        }
    }
    private List<Answer> answers;
    public List<Answer> Answers
    {
        get
        {
            if (answers == null)
                answers = (List<Answer>)answersObjSource.Select();
            return answers;
        }
        set
        {
            answers = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void answersObjSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        if (TestID == 0)
            e.Cancel = true;
        e.InputParameters["testID"] = TestID;
    }

    protected void answersObjSource_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        if (TestID == 0)
            e.Cancel = true;
        e.InputParameters["testID"] = TestID;
    }

    protected void lvwAnswers_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        e.Cancel = true;
        int answerId = Convert.ToInt32(e.Keys["AnswerID"]);
        var currAnswer = Answers.Find(ans => ans.AnswerID == answerId);
        currAnswer.Body = e.NewValues["Body"] == null ? null : e.NewValues["Body"].ToString();
        currAnswer.AnswerOrder = Convert.ToInt32(e.NewValues["AnswerOrder"]);
        currAnswer.IsCorrect = Convert.ToBoolean(e.NewValues["IsCorrect"]);
        currAnswer.Update();
        lvwAnswers.EditIndex = -1;
        lvwAnswers.DataBind();
        Answers = null;
    }

    protected void lvwAnswers_ItemInserted(object sender, ListViewInsertedEventArgs e)
    {
        Answers = null;
    }

    protected void lvwAnswers_ItemDeleted(object sender, ListViewDeletedEventArgs e)
    {
        Answers = null;
    }

    protected void lvwAnswers_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        /*if (e.CommandName == "Restore")
        {
            int testId = Convert.ToInt32(e.CommandArgument);
            var currTest = Tests.Find(tst => tst.TestID == testId);
            currTest.Deleted = false;
            currTest.Update();
            lvwTests.DataBind();
            Tests = null;
        }*/
        if (e.CommandName == "AddAnswers")
        {
            int testId = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("ManageAnswers.aspx?testID=" + testId + "#insert");
        }
        if (e.CommandName == "ShowAllAnswers")
        {
            int testId = Convert.ToInt32(e.CommandArgument);
            Response.Redirect("ManageAnswers.aspx?subjectID=" + testId);
        }
    }
    protected void lvwAnswers_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        var lblIsCorrect = e.Item.FindControl("lblIsCorrect") as Label;
        if (lblIsCorrect != null) 
            lblIsCorrect.Text = Utils.BoolToYesNo(Convert.ToBoolean(lblIsCorrect.Text));
    }
}
